package com.deer.wms.base.system.dao.zh;

import com.deer.wms.base.system.model.zh.GroupBoxBarElectricBillModel;
import com.deer.wms.base.system.model.zh.GroupBoxBillOutModel;
import com.deer.wms.base.system.model.zh.PdaSysUserModel;
import com.deer.wms.system.domain.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IExceptionGroupBoxMapper {
    /***
     * 查询用户信息
     */
    @Select("SELECT user_id,user_name FROM sys_user WHERE login_name = #{login_name}")
    PdaSysUserModel selectUser(@Param("login_name") String login_name);

    /***
     * 查询所有的SAP退货单据
     */
    @Select("SELECT bill_in_sap_detail_id,MATNR,VOLUM,CHARG " +
            "FROM bill_in_sap_detail where VOLUM!='0' and bill_in_sap_id in ((SELECT bill_in_sap_id FROM bill_in_sap where LFART='ELR'))")
    List<GroupBoxBillOutModel> findGroupBoxBillOut();

    /***
     * 查询当前单据已经组箱数量
     * @param Bill_in_sap_detail_id SAP单据明细ID
     */
    @Select("SELECT COUNT(groupbox_id) FROM box_item_exception_groupbox_detail WHERE bill_in_sap_detail_id=#{bill_in_sap_detail_id}")
    int findGroupBox(@Param("bill_in_sap_detail_id") int Bill_in_sap_detail_id);

    /***
     * 查询当前晶棒的电子箱单明细和退货单据明细中的交货单号,批次号,物料编码
     */
//    @Select("SELECT TOP (1) electric_box_detail.Conductive,electric_box_detail.F_Degree,electric_box_detail.Weight,bill_in_sap_detail.MATNR " +
//            "FROM electric_box_detail WHERE " +
//            "LEFT JOIN Coding=#{coding}" +
//            "bill_in_sap_detail on bill_in_sap_detail.VBELN=electric_box_detail.conductive")
    @Select("SELECT exception_id,materialProduct,materialBatchId,weight,state  " +
            "FROM box_item_exception " +
            "WHERE materialId=#{materialId} ")
    GroupBoxBarElectricBillModel findBarElectricBill(@Param("materialId") String coding);

    /***
     * 修改异常库区状态
     */
    @Update("UPDATE box_item_exception SET state=3 WHERE exception_id=#{eid}")
    int updateExceptionState(@Param("eid") Integer exception_id);

    /***
     * 插入groupbox_time
     */
    @Insert("INSERT INTO box_item_exception_groupbox_detail(groupbox_id,bill_in_sap_detail_id,user_id,Weight,groupbox_time,upload,F_Degree,MATNR)" +
            "VALUES(#{groupbox_id},#{bill_in_sap_detail_id},#{user_id},#{Weight},GETDATE(),0,#{F_Degree},#{MATNR})")
    int insertExceptionGroupbox(@Param("groupbox_id") String groupbox_id, @Param("bill_in_sap_detail_id") String bill_in_sap_detail_id, @Param("user_id") String user_id, @Param("Weight") double Weight, @Param("F_Degree") String F_Degree, @Param("MATNR") String MATNR);

    /***
     * 插入请求组箱退货表明细
     * @param exception_id 异常晶棒ID
     * @param groupbox_id 组箱生成ID

     */
    @Insert("INSERT INTO box_item_exception_Groupbox(exception_id,groupbox_id) " +
            "VALUES(#{exception_id},#{groupbox_id})")
    int insertExceptionGroupboxDetail(@Param("exception_id") Integer exception_id, @Param("groupbox_id") String groupbox_id);
}
